#!/bin/bash

yum_dir='/etc/yum.repos.d'
yum_dir_backup='/etc/yum.repos.d.backup'
choice=''
repo_id=''
repo_name=''
declare -A mirrors
declare -A dockers
declare -A yums
mirrors[1]='Aliyun'
mirrors[2]='Tencent'
mirrors[3]='Huawei'
mirrors[4]='USTC'

yums[1]='https://mirrors.aliyun.com/rockylinux/8/BaseOS/x86_64/os/'
yums[2]='https://mirrors.cloud.tencent.com/rocky/8/BaseOS/x86_64/os/'
yums[4]='https://mirrors.ustc.edu.cn/rocky/8/BaseOS/x86_64/os/'

appstreams[1]='https://mirrors.aliyun.com/rockylinux/8/AppStream/x86_64/os/'
appstreams[2]='https://mirrors.cloud.tencent.com/rocky/8/AppStream/x86_64/os/'
appstreams[4]='https://mirrors.ustc.edu.cn/rocky/8/AppStream/x86_64/os/'

epels[1]='https://mirrors.aliyun.com/epel/8/Everything/x86_64/'
epels[2]='https://mirrors.cloud.tencent.com/epel/8/Everything/x86_64/'
epels[3]='https://mirrors.huaweicloud.com/epel/8/Everything/x86_64/'
epels[4]='https://mirrors.ustc.edu.cn/epel/8/Everything/x86_64/'

dockers[1]='https://mirrors.aliyun.com/docker-ce/linux/rhel/8/x86_64/stable/'
dockers[2]='https://mirrors.ustc.edu.cn/docker-ce/linux/rhel/8/x86_64/stable/'
dockers[3]='https://mirrors.huaweicloud.com/docker-ce/linux/rhel/8/x86_64/stable/'
dockers[4]='https://mirrors.ustc.edu.cn/docker-ce/linux/rhel/8/x86_64/stable/'

yum_repos_backup() {
    if [ ! -d "$yum_dir_backup" ]; then
        mv $yum_dir $yum_dir_backup
        mkdir "$yum_dir"
    else
        cp -f $yum_dir/$1 $yum_dir_backup/$1 &>/dev/null
        echo "$1 备份完成"
    fi
}

_help() {
    echo -e "Options:"
    echo -e " yum\t -- \t配置Yum源"
    echo -e " epel\t -- \t配置EPEL源"
    echo -e " docker\t -- \t配置Docker源"
    echo -e " zabbix\t -- \t配置Zabbix源"
    echo
}

tip() {
    echo -e "\n 阿里云\t\t1)"
    echo -e " 腾讯云\t\t2)"
    echo -e " 华为云\t\t3)"
    echo -e " 中科大\t\t4)\n"
    read -rp "选择镜像源[ 1-10 ]: " choice

}

config_docker() {
    echo -e "\n 阿里云\t\t\t1)"
    echo -e " 腾讯云(不支持)\t\t2)"
    echo -e " 华为云\t\t\t3)"
    echo -e " 中科大\t\t\t4)\n"
    read -rp "选择镜像源[ 1-10 ]: " choice
    while [ "$choice" = "2" ];do
        echo -e "\n\e[31m腾讯云没有docker 源,请重新选择\e[0m\n"
        read -rp "选择镜像源[ 1-10 ]: " choice
    done

    repo="${mirrors[$choice]}"
    yum_repos_backup docker-ce.repo
    cat >$yum_dir/docker-ce.repo <<EOF
[Docker-$repo]
name=Docker - $repo
baseurl=${dockers[$choice]}
gpgcheck=0
EOF

}

config_yum() {
    tip
    while [ "$choice" = "3" ];do
        echo -e "\n\e[31m华为云没有rockylinux 源,请重新选择\e[0m\n"
        read -rp "选择镜像源[ 1-10 ]: " choice
    done
    repo="${mirrors[$choice]}"

    yum_repos_backup base.repo
    cat >$yum_dir/base.repo <<EOF
[BaseOS-$repo]
name=BaseOS - $repo
baseurl=${yums[$choice]}
gpgcheck=0

[AppStream-$repo]
name=AppStream - $repo
baseurl=${appstreams[$choice]}
gpgcheck=0
EOF

    echo -e "\e[32myum源修改成功\e[0m\n"
    dnf repolist

}

config_epel(){
    tip
    repo="${mirrors[$choice]}"
    cat >$yum_dir/epel.repo <<EOF
[EPEL-$repo]
name=EPEL - $repo
baseurl=${epels[$choice]}
gpgcheck=0
EOF
}

main() {
    [ ! "$1" ] && {
        _help
        exit
    }

    case "$1" in
        yum)
            config_yum;;
        docker)
            config_docker;;
        epel)
            config_epel;;
        zabbix)
            config_epel;;
    esac

}

clear
main "$1"

# if main; then
# echo -e "\n\e[32msuccess \e[0m"
# fi
